//
//  MyCell.m
//  FoldTableViewCellDemo
//
//  Created by lixiaoyi on 2018/7/13.
//  Copyright © 2018年 Anchoriter. All rights reserved.
//

#import "Masonry.h"
#import "ChatHeader.h"
#import "YZSystemMessageCell.h"
#import "YZSystemMessageModel.h"

@implementation YZSystemMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createViews];
        [self layoutViews];
    }
    
    return self;
}

- (void)createViews {
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.foldBtn];
}

- (void)layoutViews {
    UIView *superView = self.contentView;
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).with.offset(12);
        make.right.mas_equalTo(self.titleLabel.mas_left).with.offset(-12);
        make.top.mas_equalTo(superView.mas_top).with.offset(12);
        make.width.height.mas_equalTo(42);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).with.offset(-12);
        make.top.mas_equalTo(superView.mas_top).with.offset(12);
        make.bottom.mas_equalTo(self.contentLabel.mas_top).with.offset(-6);
        make.width.mas_equalTo(120);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(superView.mas_right).with.offset(-12);
        make.top.mas_equalTo(superView.mas_top).with.offset(12);
        make.bottom.mas_equalTo(self.contentLabel.mas_top).with.offset(-6);
        make.width.mas_equalTo(120);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).with.offset(12);
        make.right.mas_equalTo(superView.mas_right).with.offset(-12);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(6);
    }];
    
    
    self.contentLabel.preferredMaxLayoutWidth = kWidth-10-10-1;
    
    [self.foldBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentLabel.mas_bottom).with.offset(0);
        make.right.mas_equalTo(superView.mas_right).with.offset(-12);
        make.width.mas_equalTo(120);
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(-10);
        
    }];
    
}


- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        CGRect imageFrame = CGRectMake(0, 0, 40, 40);
        _iconImageView = [[UIImageView alloc] initWithFrame:imageFrame];
    }
    
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        [_titleLabel setTextColor:[UIColor blackColor]];
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    
    return _titleLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        [_timeLabel setTextColor:[UIColor grayColor]];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _timeLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        [_contentLabel setTextColor:[UIColor grayColor]];
        _contentLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _contentLabel;
}

- (UIButton *)foldBtn {
    if (!_foldBtn) {
        _foldBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_foldBtn setTitle:@"" forState:UIControlStateNormal];
        [_foldBtn addTarget:self action:@selector(foldBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_foldBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 80, 0, 0)];
    }
    
    return _foldBtn;
}

- (void)foldBtnClick:(UIButton *)sender {
    
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(clickFoldBtn:)]) {
        
        [self.cellDelegate clickFoldBtn:self];
    }
    
}


-(void)setMyModel:(YZSystemMessageModel *)myModel{
    _myModel = myModel;
    
    self.contentLabel.text = myModel.desc;
    // 可以在这里修改行间距，下面在计算文本高度的时候也要对应设置
    // 如果不需要修改，可以省去这一步，但注意下面获取高度的时候不要再设置行间距
    if (self.contentLabel.text.length > 0) {
        NSMutableAttributedString *img_text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", myModel.desc]];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3];
        [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [img_text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.contentLabel.text.length)];
        
        self.contentLabel.attributedText = img_text;
    }
    // 获取文本内容宽度，计算展示全部文本所需高度
    CGFloat contentW = kWidth-2*10 ;
    NSString *contentStr = self.contentLabel.text;
    
    NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
    [descStyle setLineSpacing:3];//行间距
    CGRect textRect = [contentStr
                       boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                       attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f], NSParagraphStyleAttributeName : descStyle}
                       context:nil];
    // 这里的高度60是通过指定显示三行文字时，通过打印得到的一个临界值，根据需要自行修改
    // 超过三行文字，折叠按钮不显示
    if (textRect.size.height > 60) {
        
        self.foldBtn.hidden = NO;
        // 修改按钮的折叠打开状态
        if (myModel.isOpening) {
            
            self.contentLabel.numberOfLines = 0;
            [self.foldBtn setTitle:@"收起" forState:UIControlStateNormal];
        }else{
            
            self.contentLabel.numberOfLines = 3;
            [self.foldBtn setTitle:@"全部" forState:UIControlStateNormal];
        }
    }else{
        
        self.contentLabel.numberOfLines = 0;
        self.foldBtn.hidden = YES;
    }
    
    NSTimeInterval time = [myModel.pubdate doubleValue];
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSString *dateStr = [dateFormatter stringFromDate:detaildate];
    if (dateStr.length > 0) {
        //日期
        self.timeLabel.text = [dateStr substringWithRange:NSMakeRange(0, 10)];
    }
    self.titleLabel.text = myModel.title;
    [self.iconImageView setImage:[UIImage imageNamed:@""]];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
